import { service } from './requestClass';
import basePz from '@/common/config';
import qs from 'qs';
console.log(service)
// 请求拦截器
service.interceptors.request((request) => {
    const { isLoading } = request
    if (isLoading) {
        uni.showLoading({
            title: '加载中',
        });
    }
    console.log(request)
    if (request.header.contentType) {
        request.header['content-type'] = request.header.contentType;
        delete request.header.contentType;
    }
    if (request.method === 'GET') {
        request.data = qs.stringify(request.data);
        request.url = request.url + '?' + request.data;
    }
    return request;
});
// 响应拦截器
service.interceptors.response((response) => {
    console.log(response)
    const token = uni.getStorageSync('token');
    const { data } = response

    uni.hideLoading();
    console.log(data)
    if (data.code == 2000) {
        return data;
    } else {
        uni.showToast({
            title: data.message,
            duration: 2000,
            icon: 'error'
        });

        return Promise.reject(data)

    }
    // if (response.data.code === 403) {
    //   uni.showToast({
    //     title: token ? '请重新登录' : '请先登录',
    //     icon: 'none',
    //     duration: 2000,
    //   })
    //   uni.removeStorageSync('token')
    //   uni.removeStorageSync('userInfo')
    // }

});

// 设置默认配置
service.setConfig((config) => {
    config.baseURL = basePz.baseUrl;
    if (uni.getStorageSync('token')) {
        config.header['Authorization'] = 'Bearer ' + uni.getStorageSync('token');
    }
    config.timeout = 25000
    return config;
});

const request = (data) => service.request(data)
export default request